package com.pansky.crm.controller.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pansky.crm.annotation.OperateLog;
import com.pansky.crm.common.mybatisplus.ApiResult;
import com.pansky.crm.common.mybatisplus.QueryWapperUtils;
import com.pansky.crm.common.mybatisplus.ResultPageDto;
import com.pansky.crm.config.redis.RedisBean;
import com.pansky.crm.entity.ImageInfo;
import com.pansky.crm.entity.bo.page.ImagePageBo;
import com.pansky.crm.service.ImageService;
import com.pansky.crm.service.impl.FileService;
import com.pansky.crm.util.TokenUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;
import java.util.List;

/**
 * @author ：zhouzhiyong
 * @date ：Created in 2021/3/9 10:37
 * @description：图片管理
 */
@Slf4j
@RestController
@RequestMapping("/image")
@OperateLog
@Api(tags = {"管理端-图片管理api"})
public class ImageController {

    @Autowired
    private ImageService imageService;

    @Autowired
    private FileService fileService;


    @Autowired
    private RedisBean redisBean;
    @PostMapping("/page")
    public ApiResult<ResultPageDto> findImagePage(@RequestBody ImagePageBo imagePageBo) {
        return ApiResult.ok(imageService.queryByPage(imagePageBo));
    }

    @PostMapping("/list")
    public ApiResult<List<ImageInfo>> findImageList(ImageInfo imageInfo) {
        log.info("---ImageController---findImageList---imageInfo:{}", imageInfo);
        QueryWrapper<ImageInfo> queryWrapper = QueryWapperUtils.queryToWrapper(imageInfo, ImageInfo.class);
        return ApiResult.ok(imageService.list(queryWrapper));
    }

    @GetMapping("/get")
    public ApiResult<ImageInfo> getImage(@RequestParam Integer imageId) {
        return ApiResult.ok(imageService.getById(imageId));
    }

    @PostMapping("/add")
    public ApiResult<Boolean> saveImage(@RequestBody ImageInfo imageInfo, @RequestHeader String token) {
        String subject = TokenUtil.getTokenBody(token).getSubject();
        redisBean.del("exist_File:" + subject);
        return ApiResult.ok(imageService.save(imageInfo));
    }

    @PostMapping("/update")
    public ApiResult<Boolean> updateImage(@RequestBody ImageInfo imageInfo, @RequestHeader String token) {
        ImageInfo oldInfo = imageService.getById(imageInfo.getId());
        if (!oldInfo.getImageUrl().equals(imageInfo.getImageUrl())) {
            try {
                if (imageInfo.getImageUrl() != null) {
                    fileService.removeFile(oldInfo.getImageUrl(), token);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        return ApiResult.ok(imageService.updateById(imageInfo));
    }

    @PostMapping("/delete")
    public ApiResult<Boolean> removeImage(HttpServletRequest request, @RequestParam Integer imageId, @RequestHeader String token) {
        Boolean result = imageService.removeImage(request, imageId);
        if (Boolean.TRUE.equals(result)) {
            String subject = TokenUtil.getTokenBody(token).getSubject();
            redisBean.del("exist_File:" + subject);
            return ApiResult.ok();
        }
        return ApiResult.failed("删除图片失败");
    }
}
